home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LTP_LayoutMenu.c < prev    next >
C/C++ Source or Header  |  1996-03-18  |  10KB  |  438 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #ifdef DO_MENUS
  15.  
  16.     /* LTP_LayoutMenu(RootMenu *Root,WORD ExtraFront,WORD ExtraSpace):
  17.      *
  18.      *    Layout the menus, menu items and submenu items.
  19.      */
  20.  
  21. BOOL
  22. LTP_LayoutMenu(RootMenu *Root,LONG ExtraFront,LONG ExtraSpace)
  23. {
  24.     LONG        MenuLeft,
  25.                 ItemTop,
  26.                 ItemWidth,
  27.                 ItemShift,
  28.                 CommandWidth,
  29.                 Width;
  30.  
  31.     MenuNode    *Menu;
  32.     ItemNode    *Item,*FirstItem = NULL,*LastItem;
  33.  
  34.     BOOL        Correction;
  35.  
  36.         // That one's simple
  37.  
  38.     for(MenuLeft = ExtraFront, Menu = (MenuNode *)Root->MenuList.mlh_Head ; Menu->Node.mln_Succ ; Menu = (MenuNode *)Menu->Node.mln_Succ)
  39.     {
  40.         Menu->Menu.LeftEdge = MenuLeft;
  41.  
  42.         MenuLeft += Menu->Menu.Width + ExtraSpace;
  43.     }
  44.  
  45.         // Now run down the list of items
  46.  
  47.     for(Item = (ItemNode *)Root->ItemList.mlh_Head ; Item->Node.mln_Succ ; Item = (ItemNode *)Item->Node.mln_Succ)
  48.     {
  49.             // Hit a submenu item?
  50.  
  51.         if(Item->Flags & ITEMF_FirstSub)
  52.         {
  53.             ItemNode    *FirstSub,*LastSub,*Sub;
  54.             LONG         SubTop            = 0,
  55.                          SubWidth        = 0;
  56.             LONG         CommandWidth    = 0;
  57.  
  58.                 // This is where we started
  59.  
  60.             FirstSub = Item;
  61.  
  62.                 // Now determine the widest entry and
  63.                 // line them up below one another
  64.  
  65.             for(Sub = FirstSub ; Sub->Node.mln_Succ && (Sub->Flags & ITEMF_IsSub) ; Sub = (ItemNode *)Sub->Node.mln_Succ)
  66.             {
  67.                 LastSub = Sub;
  68.  
  69.                 if(Sub->Item.Width > SubWidth)
  70.                     SubWidth = Sub->Item.Width;
  71.  
  72.                 if((Width = LTP_GetCommandWidth(Root,Sub)) > CommandWidth)
  73.                     CommandWidth = Width;
  74.  
  75.                 Sub->Item.TopEdge = SubTop;
  76.  
  77.                 SubTop += Sub->Item.Height;
  78.             }
  79.  
  80.             SubWidth += 4 + CommandWidth;
  81.  
  82.                 // In the second pass, make all entries
  83.                 // use the same width
  84.  
  85.             for(Sub = FirstSub ; ; Sub = (ItemNode *)Sub->Node.mln_Succ)
  86.             {
  87.                     // Indent the entries that need it
  88.  
  89.                 if(Sub->Item.Flags & CHECKIT)
  90.                     ((struct IntuiText *)Sub->Item.ItemFill)->LeftEdge += 2 + Root->CheckWidth;
  91.  
  92.                 Sub->Item.Width = SubWidth;
  93.  
  94.                     // Adapt the separator bar size
  95.  
  96.                 if(Sub->Flags & ITEMF_IsBar)
  97.                     ((struct Image *)Sub->Item.ItemFill)->Width = Sub->Item.Width - 4;
  98.                 else
  99.                 {
  100.                         // Take care of command keys
  101.  
  102.                     if(Sub->Flags & ITEMF_Command)
  103.                     {
  104.                         struct IntuiText *Command = ((struct IntuiText *)Sub->Item.ItemFill)->NextText;
  105.  
  106.                         Command->LeftEdge = Sub->Item.Width - (IntuiTextLength(Command) + 2);
  107.                     }
  108.                 }
  109.  
  110.                     // In the next iteration, continue after the last
  111.                     // submenu item
  112.  
  113.                 if(Sub == LastSub)
  114.                 {
  115.                     Item = Sub;
  116.  
  117.                     break;
  118.                 }
  119.             }
  120.  
  121.             DB(kprintf("last sub |%s|\n",((struct IntuiText *)Item->Item.ItemFill)->IText));
  122.         }
  123.         else
  124.         {
  125.                 // Start a new menu?
  126.  
  127.             if(!FirstItem)
  128.             {
  129.                     // This is where we started
  130.  
  131.                 FirstItem = Item;
  132.  
  133.                     // Reset the data
  134.  
  135.                 ItemTop = ItemWidth = ItemShift = CommandWidth = 0;
  136.             }
  137.  
  138.                 // Line up the entries in a column
  139.  
  140.             Item->Item.TopEdge = ItemTop;
  141.  
  142.             ItemTop += Item->Item.Height;
  143.  
  144.                 // Search for the widest entry
  145.  
  146.             if(Item->Item.Width > ItemWidth)
  147.                 ItemWidth = Item->Item.Width;
  148.  
  149.                 // Search for the widest command sequence
  150.  
  151.             if((Width = LTP_GetCommandWidth(Root,Item)) > CommandWidth)
  152.                 CommandWidth = Width;
  153.  
  154.                 // This is for submenu items which will get
  155.                 // indented by this amount
  156.  
  157.             if(Item->Item.Width > ItemShift)
  158.                 ItemShift = Item->Item.Width;
  159.         }
  160.  
  161.             // Is this the last item for this menu?
  162.  
  163.         if(Item->Flags & ITEMF_LastItem)
  164.         {
  165.             ItemWidth += 4 + CommandWidth;
  166.  
  167.                 // Restart and layout all the items in this menu
  168.  
  169.             for(Item = FirstItem ; Item->Node.mln_Succ ; Item = (ItemNode *)Item->Node.mln_Succ)
  170.             {
  171.                     // Indent submenu items
  172.  
  173.                 if(Item->Flags & ITEMF_IsSub)
  174.                     Item->Item.LeftEdge += ItemShift + 6;
  175.                 else
  176.                 {
  177.                         // Indent items as necessary
  178.  
  179.                     if(Item->Item.Flags & CHECKIT)
  180.                         ((struct IntuiText *)Item->Item.ItemFill)->LeftEdge += 2 + Root->CheckWidth;
  181.  
  182.                     Item->Item.Width = ItemWidth;
  183.  
  184.                         // Adapt the separator bar size
  185.  
  186.                     if(Item->Flags & ITEMF_IsBar)
  187.                         ((struct Image *)Item->Item.ItemFill)->Width = Item->Item.Width - 4;
  188.                     else
  189.                     {
  190.                             // Take care of submenu item indicators and
  191.                             // command sequences
  192.  
  193.                         if(Item->Flags & (ITEMF_Command | ITEMF_HasSub))
  194.                         {
  195.                             struct IntuiText *Command = ((struct IntuiText *)Item->Item.ItemFill)->NextText;
  196.  
  197.                             Command->LeftEdge = Item->Item.Width - (IntuiTextLength(Command) + 2);
  198.                         }
  199.                     }
  200.                 }
  201.  
  202.                     // Abort if this is the last item for this menu
  203.  
  204.                 if(Item->Flags & ITEMF_LastItem)
  205.                     break;
  206.             }
  207.  
  208.             DB(kprintf("last item |%s|\n",((struct IntuiText *)Item->Item.ItemFill)->IText));
  209.  
  210.                 // The next iteration will start a new menu
  211.  
  212.             FirstItem = NULL;
  213.         }
  214.     }
  215.  
  216.         // Calculate the effective positions
  217.  
  218.     LTP_AdjustMenuPosition(Root);
  219.  
  220.     Correction = FALSE;
  221.  
  222.         // We start by chopping down the menus
  223.  
  224.     for(Item = (ItemNode *)Root->ItemList.mlh_Head ; Item->Node.mln_Succ ; Item = (ItemNode *)Item->Node.mln_Succ)
  225.     {
  226.             // That's where we start
  227.  
  228.         if(!FirstItem)
  229.             FirstItem = Item;
  230.  
  231.             // Skip submenu items
  232.  
  233.         if(!(Item->Flags & ITEMF_IsSub))
  234.             LastItem = Item;
  235.  
  236.             // Did we reach the end of the menu?
  237.  
  238.         if(Item->Flags & ITEMF_LastItem)
  239.         {
  240.             DB(kprintf("chopping |%s|->|%s|\n",((struct IntuiText *)FirstItem->Item.ItemFill)->IText,((struct IntuiText *)LastItem->Item.ItemFill)->IText));
  241.  
  242.                 // Chop down the menus
  243.  
  244.             Correction |= LTP_CorrectItemList(Root,FirstItem,LastItem);
  245.  
  246.             FirstItem = NULL;
  247.         }
  248.     }
  249.  
  250.     if(Correction)
  251.     {
  252.         Correction = FALSE;
  253.  
  254.             // Recalculate the positions
  255.  
  256.         LTP_AdjustMenuPosition(Root);
  257.     }
  258.  
  259.         // Next we shift the menus around to make them fit
  260.  
  261.     for(Menu = (MenuNode *)Root->MenuList.mlh_Head ; Menu->Node.mln_Succ ; Menu = (MenuNode *)Menu->Node.mln_Succ)
  262.     {
  263.         MenuLeft = Menu->Menu.LeftEdge + 4 + Menu->Width + 4;
  264.  
  265.             // Does it cross the right screen border?
  266.  
  267.         if(MenuLeft > Root->Screen->Width)
  268.         {
  269.                 // Is the menu wider than the screen?
  270.  
  271.             if(4 + Menu->Width + 4 > Root->Screen->Width)
  272.             {
  273.                 DB(kprintf("menu too wide\n"));
  274.                 return(FALSE);
  275.             }
  276.             else
  277.             {
  278.                 LONG Left = MenuLeft - Root->Screen->Width + 1;
  279.  
  280.                     // Move up
  281.  
  282.                 for(Item = (ItemNode *)((ULONG)Menu->Menu.FirstItem - sizeof(struct MinNode)) ; Item->Node.mln_Succ ; Item = (ItemNode *)Item->Node.mln_Succ)
  283.                 {
  284.                     if(!(Item->Flags & ITEMF_IsSub))
  285.                         Item->Item.LeftEdge -= Left;
  286.  
  287.                     if(Item->Flags & ITEMF_LastItem)
  288.                         break;
  289.                 }
  290.  
  291.                 Correction = TRUE;
  292.             }
  293.         }
  294.     }
  295.  
  296.     if(Correction)
  297.     {
  298.         Correction = FALSE;
  299.  
  300.             // Recalculate the positions
  301.  
  302.         LTP_AdjustMenuPosition(Root);
  303.     }
  304.  
  305.         // Now deal with the submenus
  306.  
  307.     for(Item = (ItemNode *)Root->ItemList.mlh_Head ; Item->Node.mln_Succ ; Item = (ItemNode *)Item->Node.mln_Succ)
  308.     {
  309.             // Only submenu items, please
  310.  
  311.         if(Item->Flags & ITEMF_FirstSub)
  312.         {
  313.             ItemNode *Here;
  314.  
  315.                 // Find the first and the last entry
  316.  
  317.             for(Here = Item ; Here->Node.mln_Succ ; Here = (ItemNode *)Here->Node.mln_Succ)
  318.             {
  319.                 if(!(Here->Flags & ITEMF_IsSub))
  320.                     break;
  321.                 else
  322.                     LastItem = Here;
  323.             }
  324.  
  325.             DB(kprintf("2) chopping |%s|->|%s|\n",((struct IntuiText *)Item->Item.ItemFill)->IText,((struct IntuiText *)LastItem->Item.ItemFill)->IText));
  326.  
  327.                 // Chop down the submenus
  328.  
  329.             Correction |= LTP_CorrectItemList(Root,Item,LastItem);
  330.         }
  331.     }
  332.  
  333.     if(Correction)
  334.     {
  335.         Correction = FALSE;
  336.  
  337.         LTP_AdjustMenuPosition(Root);
  338.     }
  339.  
  340.         // Almost finished, now shift the submenus around
  341.  
  342.     for(Item = (ItemNode *)Root->ItemList.mlh_Head ; Item->Node.mln_Succ ; Item = (ItemNode *)Item->Node.mln_Succ)
  343.     {
  344.             // Did we hit a submenu?
  345.  
  346.         if(Item->Flags & ITEMF_HasSub)
  347.         {
  348.             ItemNode *Here = (ItemNode *)((ULONG)Item->Item.SubItem - sizeof(struct MinNode));
  349.  
  350.                 // Does this one also cross the right screen border?
  351.  
  352.             if((MenuLeft = Here->Left + 4 + Here->Item.Width + 4) > Root->Screen->Width)
  353.             {
  354.                 LONG Left = MenuLeft - Root->Screen->Width;
  355.  
  356.                     // Check if the subitem would cover too much
  357.                     // of the item it is attached to
  358.  
  359.                 if(Here->Left - Left < (Item->Left + 4 + Item->Width - CommandWidth))
  360.                 {
  361.                     Left = Here->Left + (4 + Here->Item.Width + 4) - Item->Left - 5;
  362.  
  363.                     if(Here->Left - Left < 0)
  364.                     {
  365.                         DB(kprintf("sub too fat\n"));
  366.                         return(FALSE);
  367.                     }
  368.                 }
  369.  
  370.                 DB(kprintf("3) shifting...\n"));
  371.  
  372.                     // Shift the menu around
  373.  
  374.                 do
  375.                 {
  376.                     if(!(Here->Flags & ITEMF_IsSub))
  377.                         break;
  378.                     else
  379.                     {
  380.                         Here->Item.LeftEdge -= Left;
  381.                         Here->Left -= Left;
  382.  
  383.                         Correction = TRUE;
  384.  
  385.                         if(Here->Flags & ITEMF_LastItem)
  386.                             break;
  387.  
  388.                         Here = (ItemNode *)Here->Node.mln_Succ;
  389.                     }
  390.                 }
  391.                 while(Here->Node.mln_Succ);
  392.             }
  393.         }
  394.     }
  395.  
  396.         // The last step; see if the alignment stuff worked
  397.  
  398.     if(Correction)
  399.         LTP_AdjustMenuPosition(Root);
  400.  
  401.         // First check the menus
  402.  
  403.     for(Menu = (MenuNode *)Root->MenuList.mlh_Head ; Menu->Node.mln_Succ ; Menu = (MenuNode *)Menu->Node.mln_Succ)
  404.     {
  405.             // Does it cross the screen borders?
  406.  
  407.         if((MenuLeft = Menu->Menu.LeftEdge + 4 + Menu->Width + 4) > Root->Screen->Width || Menu->Menu.LeftEdge < 0)
  408.         {
  409.             DB(kprintf("menu crosses screen border\n"));
  410.             return(FALSE);
  411.         }
  412.     }
  413.  
  414.         // Now check the submenus
  415.  
  416.     for(Item = (ItemNode *)Root->ItemList.mlh_Head ; Item->Node.mln_Succ ; Item = (ItemNode *)Item->Node.mln_Succ)
  417.     {
  418.             // Did we hit a submenu?
  419.  
  420.         if(Item->Flags & ITEMF_HasSub)
  421.         {
  422.             ItemNode *Here = (ItemNode *)((ULONG)Item->Item.SubItem - sizeof(struct MinNode));
  423.  
  424.             if(Here->Left + Here->Width > Root->Screen->Width)
  425.             {
  426.                 DB(kprintf("submenu crosses screen border\n"));
  427.                 return(FALSE);
  428.             }
  429.         }
  430.     }
  431.  
  432.     LTP_FillMenu(&Root->Menu);
  433.  
  434.     return(TRUE);
  435. }
  436.  
  437. #endif    /* DO_MENUS */
  438.